package com.banmao.algorithm.No201To300.No226;

import java.util.ArrayDeque;

public class Solution1 {

    public TreeNode invertTree(TreeNode root) {

        ArrayDeque<TreeNode> queue = new ArrayDeque<>();
        if(root == null) {
            return null;
        }

        queue.add(root);
        while (!queue.isEmpty()) {
            int size = queue.size();

            for (int i = 0; i < size; i++) {
                TreeNode poll = queue.poll();
                TreeNode left = poll.left;
                poll.left = poll.right;
                poll.right = left;

                if(poll.left != null) {
                    queue.add(poll.left);
                }
                if(poll.right != null) {
                    queue.add(poll.right);
                }
            }
        }
        return root;

    }

}

class TreeNode {
    int val;
    TreeNode left;
    TreeNode right;
    TreeNode() {}
    TreeNode(int val) { this.val = val; }
    TreeNode(int val, TreeNode left, TreeNode right) {
        this.val = val;
        this.left = left;
        this.right = right;
    }
}
